<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04猜数字练习</title>
</head>
<body>
<h3>已生成一个1~100之间的随机数，请猜猜看吧！</h3>
<input type="text" placeholder="输入数字">
<button onclick="guess()">猜</button>
<h3>小提示:
    <span class="tip"></span>
</h3>
    <script>
        // 1.生成随机数
        //let num = Math.floor(Math.random()*100)+1;
        let n = parseInt(Math.random()*100)+1;// 为什么要+1?   [0,100)+1 => [1,101)
        console.log('随机数为:'+n);
        // 2.定义一个累计次数的变量
        let count = 0;
        function guess() {
            count++;
            let input = parseInt(document.querySelector('input').value);
            console.log(input);
            let span = document.querySelector('.tip');
                if (isNaN(input)){
                    alert('不能为空或非数字');
                    return;
                }
                if (input === n){
                    span.innerHTML = '恭喜你猜对了,你一共猜了'+count+'次';
                    count = 0;
                }
                if (input > n){
                    span.innerHTML = '猜大了';

                }
                if (input < n){
                    span.innerHTML = '猜小了';
                }
        }


    </script>
</body>
</html>